﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Cust.STR.PlugBE.Public;
using UFSoft.UBF.Business;
using static UFIDA.U9.Cust.STR.PlugBE.Public.Dto.OAItemAddPara;
using System.Net;
using UFIDA.U9.Cust.STR.PlugBE.Public.Dto;

namespace UFIDA.U9.Cust.STR.PlugBE.Itemmaster
{
    public class AfterInserting : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            UFIDA.U9.CBO.SCM.Item.ItemMaster holder = key.GetEntity() as UFIDA.U9.CBO.SCM.Item.ItemMaster;
            if (holder == null)
                return;
            #endregion

            JsonConfig jsonConfig = new JsonConfig();
            OAItemAddPara oAItemAddPara = new OAItemAddPara();
            HttpHelper httpHelper = new HttpHelper();
            oAItemAddPara.header = httpHelper.OAPurMallToken();
            oAItemAddPara.data = new List<OAItemAddPara.Datum>()
            {
                new OAItemAddPara.Datum
                {
                    mainTable = new Maintable
                    {
                        Code = holder.Code,
                        Name = holder.Name,
                        SPECS = holder.SPECS,
                        ItemFormAttribute = holder.ItemFormAttribute.Value,
                        IsInventoryEnable = Convert.ToInt32(holder.IsInventoryEnable),
                        IsPurchaseEnable = Convert.ToInt32(holder.IsPurchaseEnable),
                        IsSalesEnable = Convert.ToInt32(holder.IsSalesEnable),
                        IsBuildEnable = Convert.ToInt32(holder.IsBuildEnable),
                        IsOutsideOperationEnable = Convert.ToInt32(holder.IsOutsideOperationEnable),
                        IsMRPEnable = Convert.ToInt32(holder.IsMRPEnable),
                        IsBOMEnable = Convert.ToInt32(holder.IsBOMEnable),
                        IsVMIEnable = Convert.ToInt32(holder.IsVMIEnable),
                        IsEffective = Convert.ToInt32(holder.Effective.IsEffective),
                        MainItemCategory = holder.MainItemCategory.Name,
                        UOM = holder.InventoryUOM.Name
                    },
                    operationinfo = new Operationinfo
                    {
                        @operator="1"
                    }
                }
            };
            httpHelper.URL = jsonConfig.SelectToken("OA.OAPurMallURL") + "/api/cube/restful/interface/saveOrUpdateModeData/cgsc";
            httpHelper.ContentType = "application/x-www-form-urlencoded";
            httpHelper.Content = $"datajson={WebUtility.UrlEncode(JsonConvert.SerializeObject(oAItemAddPara))}";
            httpHelper.Request();
        }
    }
}
